package com.lib.krismile.ui.web;

import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.BridgeWebViewClient;


public class IBridgeWebViewClient extends BridgeWebViewClient {

    public IBridgeWebViewClient(BridgeWebView webView) {
        super(webView);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        try {
            //  url = URLDecoder.decode(url, "UTF-8");
            LogUtils.d("api", "url:" + url);
            if (url.startsWith("alipays")) {
                if (isAliPayInstalled(view.getContext())) {
                    startAliPay(view.getContext(), url);
                } else {
                    ToastUtils.showLong("请安装支付宝");
                }
                return true;
            } else if (url.startsWith("tel:")) {
                call(view.getContext(), url);
                return true;
            } else if (url.startsWith("mqqwpa:")) {
                startAliPay(view.getContext(), url);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.shouldOverrideUrlLoading(view, url);

    }

    public static void call(Context context, String tel) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        if (context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
            Uri data = Uri.parse(tel);
            intent.setData(data);
            try {
                context.startActivity(intent);
            } catch (ActivityNotFoundException ex) {

            }
        }

    }


    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    public boolean isAliPayInstalled(Context context) {
        Uri uri = Uri.parse("alipays://platformapi/startApp");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        @SuppressLint("QueryPermissionsNeeded") ComponentName componentName = intent.resolveActivity(context.getPackageManager());
        return componentName != null;
    }

    public void startAliPay(Context context, String url) {
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        WebSettings settings = view.getSettings();
        settings.setBlockNetworkImage(false);
        if (!settings.getLoadsImagesAutomatically()) {
            //设置wenView加载图片资源
            settings.setLoadsImagesAutomatically(true);
        }
        super.onPageFinished(view, url);

    }


}
